package cq.zhcqk.fragment._2_dj;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
//import com.mrw.wzmrecyclerview.Divider.BaseItemDecoration;
//import com.mrw.wzmrecyclerview.HeaderAndFooter.OnItemClickListener;
//import com.mrw.wzmrecyclerview.HeaderAndFooter.OnItemLongClickListener;
//import com.mrw.wzmrecyclerview.PullToLoad.OnLoadListener;
//import com.mrw.wzmrecyclerview.PullToLoad.PullToLoadRecyclerView;
//import com.mrw.wzmrecyclerview.PullToRefresh.OnRefreshListener;
//import com.mrw.wzmrecyclerview.SimpleAdapter.SimpleAdapter;
//import com.mrw.wzmrecyclerview.SimpleAdapter.ViewHolder;
import com.jimi_wu.ptlrecyclerview.Divider.BaseItemDecoration;
import com.jimi_wu.ptlrecyclerview.HeaderAndFooter.OnItemClickListener;
import com.jimi_wu.ptlrecyclerview.HeaderAndFooter.OnItemLongClickListener;
import com.jimi_wu.ptlrecyclerview.PullToLoad.OnLoadListener;
import com.jimi_wu.ptlrecyclerview.PullToLoad.PullToLoadRecyclerView;
import com.jimi_wu.ptlrecyclerview.PullToRefresh.OnRefreshListener;
import com.jimi_wu.ptlrecyclerview.SimpleAdapter.SimpleAdapter;
import com.jimi_wu.ptlrecyclerview.SimpleAdapter.ViewHolder;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Map;

import cq.zhcqk.R;
import cq.zhcqk.activity.DY_DyqfActivity;
import cq.zhcqk.activity.DY_DyxxActivity;
import cq.zhcqk.activity.DY_DzbdtActivity;
import cq.zhcqk.activity.DY_DzbjgActivity;
import cq.zhcqk.activity.ListDetailSampleActivity;
import cq.zhcqk.util.AsyncHttpClientUtils;
import cq.zhcqk.util.Common;
import cq.zhcqk.util.GsonUtil;
import cq.zhcqk.util.ImageLoaderHelper;
import cq.zhcqk.util.StringUtil;

/**
 * Created by zhangle on 2017/11/25.
 */
public class _2_DyzjFragment extends Fragment implements View.OnClickListener{
    private View view;
    private View headerView;
    private Context context;
    private AsyncHttpClient httpClient;
    private PullToLoadRecyclerView rcv;
    private int page = 1;
    private String type = "6";
    private ArrayList<Map<String, Object>> list;

    private LinearLayout dyzj_lay1;
    private LinearLayout dyzj_lay2;
    private LinearLayout dyzj_lay3;
    private LinearLayout dyzj_lay1_2;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(view==null){
            view = inflater.inflate(R.layout.fragment_dyzj_2_2, container, false);
            httpClient = AsyncHttpClientUtils.getInstance().getAsyncHttpClient();
            context = getActivity();
            initView();
            initRecyclerView();
//            getCode(page);
        }
        ViewGroup parent=(ViewGroup)view.getParent();
        if(parent!=null){
            parent.removeView(view);
        }
        return view;
    }

    private void initView(){
        headerView = LayoutInflater.from(context).inflate(R.layout.header_dyzj_fragment,null);
        dyzj_lay1 = (LinearLayout) headerView.findViewById(R.id.dyzj_lay1);
        dyzj_lay2 = (LinearLayout) headerView.findViewById(R.id.dyzj_lay2);
        dyzj_lay3 = (LinearLayout) headerView.findViewById(R.id.dyzj_lay3);
        dyzj_lay1_2 = (LinearLayout) headerView.findViewById(R.id.dyzj_lay1_2);

        dyzj_lay1.setOnClickListener(this);
        dyzj_lay2.setOnClickListener(this);
        dyzj_lay3.setOnClickListener(this);
        dyzj_lay1_2.setOnClickListener(this);
    }

    private void initRecyclerView(){
        rcv = (PullToLoadRecyclerView)view.findViewById(R.id.rcv);

        rcv.setLayoutManager(new GridLayoutManager(context,1));
        rcv.setLoadEnable(false);
        rcv.setRefreshEnable(false);
//        设置适配器，封装后的适配器只需要实现一个函数
        list = new ArrayList<>();
        rcv.setAdapter(new SimpleAdapter<Map<String, Object>>(context, list, R.layout.item_list_recycler) {
            @Override
            protected void onBindViewHolder(ViewHolder holder, Map<String, Object> data) {
                String allowPing = data.get("allowPing")+"";
                if("0.0".equals(allowPing)){
                    holder.<ImageView>getView(R.id.item_header_img).setVisibility(View.VISIBLE);
                    holder.<ImageView>getView(R.id.item_content_img).setVisibility(View.GONE);
                    ImageLoader.getInstance().displayImage(StringUtil.checkImgeUrl(data.get("categories")+""), holder.<ImageView>getView(R.id.item_header_img), ImageLoaderHelper.getOptionsDf());
                }else if("2.0".equals(allowPing)){
                    holder.<ImageView>getView(R.id.item_header_img).setVisibility(View.GONE);
                    holder.<ImageView>getView(R.id.item_content_img).setVisibility(View.GONE);
                }else if("3.0".equals(allowPing)){
                    holder.<ImageView>getView(R.id.item_header_img).setVisibility(View.GONE);
                    holder.<ImageView>getView(R.id.item_content_img).setVisibility(View.VISIBLE);
                    ImageLoader.getInstance().displayImage(StringUtil.checkImgeUrl(data.get("categories")+""), holder.<ImageView>getView(R.id.item_content_img), ImageLoaderHelper.getOptionsDf());
                }
                holder.<TextView>getView(R.id.item_title).setText(""+data.get("title"));
                holder.<TextView>getView(R.id.item_num).setText(""+ StringUtil.stringToInt(data.get("hits")+""));
                holder.<TextView>getView(R.id.item_time).setText(""+data.get("time"));
            }
        });

        rcv.addHeaderView(headerView);
//        设置刷新监听
        rcv.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onStartRefreshing() {
            }
        });
//        设置加载监听
        rcv.setOnLoadListener(new OnLoadListener() {
            @Override
            public void onStartLoading(int skip) {
                getCode(page);
            }
        });
//        设置分割线
        rcv.addItemDecoration(new BaseItemDecoration(context,R.color.gray_bg));

        rcv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void OnItemClick(int position) {
                Intent intent = new Intent(context, ListDetailSampleActivity.class);
                intent.putExtra("cid",(int) Double.parseDouble(String.valueOf(list.get(position).get("cid")))+"");
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
            }
        });
        rcv.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(int position) {
                return true;
            }
        });
    }

    private void getCode(int offset){
        RequestParams params = new RequestParams();
        params.add("limit",Common.PAGE_SIZE);
        params.add("offset",offset+"");
        params.add("type",type);
        httpClient.post(Common.URL+Common.NEWS_LIST,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
                ArrayList<Map<String, Object>> listNew = null;
                listNew = (ArrayList<Map<String, Object>>)((Map<String, Object>)map.get("data")).get("rows");
                if(listNew == null || listNew.size()==0){
                    Toast.makeText(context,"没有更多",Toast.LENGTH_SHORT).show();
                }else{
                    list.addAll(listNew);
                    page ++ ;
                }
                rcv.completeRefresh();
                rcv.completeLoad(1);

            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                System.out.println("onFailure："+errorResponse);
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()){
            case R.id.dyzj_lay1:
                intent = new Intent(context, DY_DzbjgActivity.class);
                context.startActivity(intent);
                getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
            case R.id.dyzj_lay2:
                intent = new Intent(context, DY_DyxxActivity.class);
                context.startActivity(intent);
                getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
            case R.id.dyzj_lay3:
                intent = new Intent(context, DY_DyqfActivity.class);
                context.startActivity(intent);
                getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
            case R.id.dyzj_lay1_2:
                intent = new Intent(context, DY_DzbdtActivity.class);
                context.startActivity(intent);
                getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
        }
    }
}
